package com.zidian.teacher.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.zidian.teacher.R;
import com.zidian.teacher.common.ActManager;
import com.zidian.teacher.common.BaseEntity;
import com.zidian.teacher.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ProgressBarBlackUtil;

/**
 * Created by Administrator on 2016/8/28 0028.
 */
public class TeacherSetPasswordActivity extends ToolBarActivity implements TextWatcher {

    @Bind(R.id.teacher_change_old_psw)
    EditText teacherChangeOldPsw;
    @Bind(R.id.teacher_change_new_psw)
    EditText teacherChangeNewPsw;
    @Bind(R.id.teacher_change_sure_psw)
    EditText teacherChangeSurePsw;
    @Bind(R.id.submit_btn)
    Button submitBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_teacher_password);
        ButterKnife.bind(this);
        setTitle("修改密码");
        initView();
    }

    private void initView() {
        teacherChangeNewPsw.addTextChangedListener(this);
        teacherChangeSurePsw.addTextChangedListener(this);
        teacherChangeOldPsw.addTextChangedListener(this);

    }

    @OnClick(R.id.submit_btn)
    public void changePassword() {
        submitBtn.setEnabled(false);
        if (teacherChangeOldPsw.getText().toString().trim().equals(teacherChangeNewPsw.getText().toString().trim())) {
            Snackbar.make(submitBtn, "新密码与旧密码不得一样", Snackbar.LENGTH_SHORT).show();
            return;
        }
        if (teacherChangeNewPsw.getText().toString().trim().length() < 6 ||
                teacherChangeNewPsw.getText().toString().trim().length() > 15) {
            Snackbar.make(submitBtn, "请输入合法长度的密码", Snackbar.LENGTH_SHORT).show();
            return;
        }
        if (teacherChangeNewPsw.getText().toString().equals(teacherChangeSurePsw.getText().toString())) {
            try {
                changePSW();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Snackbar.make(submitBtn, "两次输入的密码不一致", Snackbar.LENGTH_SHORT).show();
        }
    }

    private void changePSW() throws IOException {
        String loginUrl = Constant.URL_TEACHER + "teacher/updatePassword";
        HashMap map = new HashMap();
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("password", teacherChangeOldPsw.getText().toString().trim());
        map.put("password1", teacherChangeNewPsw.getText().toString().trim());
        map.put("password2", teacherChangeSurePsw.getText().toString().trim());
        map.put("token", GetUserInfo.getToken(context));
        ProgressBarBlackUtil.loadShow(context);
        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("E", e.toString());
                ProgressBarBlackUtil.loadCancel();
            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                BaseEntity base = gson.fromJson(response, BaseEntity.class);
                if (base.isSuccess()) {
                    ProgressBarBlackUtil.loadCancel();
                    ToastUtil.showToast(context, "提交成功");
                    ActManager.getAppManager().finishAllActivity();
                    GetUserInfo.loginOut(context);
                    startActivity(LoginActivity.class);
                } else {
                    ToastUtil.showToast(context, base.getMessage());
                    ProgressBarBlackUtil.loadCancel();
                }
            }
        });

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        boolean newpsw = teacherChangeNewPsw.getText().toString().trim().length() > 0;
        boolean surepsw = teacherChangeSurePsw.getText().toString().trim().length() > 0;
        boolean oldpsw = teacherChangeOldPsw.getText().toString().trim().length() > 0;

        if (newpsw && oldpsw && surepsw) {
            submitBtn.setEnabled(true);
        } else {
            submitBtn.setEnabled(false);
        }


    }

    @Override
    public void afterTextChanged(Editable s) {

    }


}
